package labox.innovation.gameserver.datatables;

import java.io.File;
import java.io.FileInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import labox.innovation.config.Config;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * 游戏升级经验数据表
 *
 */
public final class Experience {

	public static int[] LEVEL;

	public static byte MAX_LEVEL;

	public Experience() {
		loadExperience();
	}

	public static Experience getInstance() {
		return SingletonHolder._instance;
	}

	private static class SingletonHolder {
		protected static final Experience _instance = new Experience();
	}

	public void loadExperience() {
		try {
			File f = new File(Config.DATAPACK_ROOT, "/data/experience.xml");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(new FileInputStream(f));
			Element root;
			root = document.getDocumentElement();
			if (root.getTagName().compareTo("Experience") != 0) {
				throw new Exception("Not a Experience file!");
			}

			MAX_LEVEL = Byte.parseByte(root.getAttribute("maxLevel"));

			NodeList nl = root.getElementsByTagName("Level");

			LEVEL = new int[nl.getLength()];

			for (int i = -1; ++i < nl.getLength();) {
				Element e = (Element) nl.item(i);
				LEVEL[i] = Integer.parseInt(e.getAttribute("experience"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
